<template>
  <view>
    <view class="zhan-wei"></view>
    <tui-tabbar class="tabbar" :tabBar="tabBar" :isFixed="true" :unlined="true" :current="current" @click="tabbarSwitch"></tui-tabbar>

    <!-- 扫码组件 -->
    <modal-scan ref="scanModal"></modal-scan>
  </view>
</template>

<script>
export default {
  props: {
    currentTab: {
      type: Number,
      default: null
    }
  },
  computed: {
    current() {
      const currentPage = this.$currentPage()
      let currentTab = 0
      const tabBar = this.tabBar
      for (let i = 0; i < tabBar.length; i++) {
        if (currentPage.indexOf(tabBar[i].pagePath) > 0) {
          currentTab = i
          break
        }
      }
      return currentTab
    },
    tabBar() {
      const tabBar = this.$store.state.TabBar.tabBar

      // 控制是否显示扫码点单
      const ifScan = this.$store.getters.ifScan
      if (!ifScan) {
        return tabBar.filter(item => {
          if (item.pagePath.indexOf('scan') === -1) return item
        })
      }

      return tabBar
    }
  },
  methods: {
    setProps(props) {
      this.$store.dispatch('TabBar/setProps', props)
    },
    tabbarSwitch(param) {
      const pagePath = this.$format(param, 'pagePath')
      if (!pagePath) return this.$msg('text', '路径不存在')
      if (pagePath.indexOf('scan') > -1) return this.openScan()

      this.$store.dispatch('TabBar/setTabBarCurrent', this.$format(param, 'index'))
      this.$open(pagePath, this.$format(param, 'openType', 'st'))
    },
    // 扫码
    openScan() {
      this.$refs.scanModal.scanRoomQrCode().then(res => {
        this.$open('/shop/shop', 'nt')
        this.$eventHub.$emit('selectBusiness', { handle: 'my-tabbar' })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.zhan-wei {
  height: 100rpx;
  height: calc(100rpx + env(safe-area-inset-bottom));
}
</style>
